home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / hplip / firmware.py < prev    next >
Text File  |  2008-10-13  |  7KB  |  245 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '1.1'
  24. __title__ = 'Firmware Download Utility'
  25. __doc__ = "Download firmware to a device."
  26.  
  27. # Std Lib
  28. import sys
  29. import getopt
  30. import gzip
  31. import operator
  32. import time
  33. import os
  34.  
  35. # Local
  36. from base.g import *
  37. from base import device, status, utils
  38. from prnt import cups
  39.  
  40. USAGE = [(__doc__, "", "name", True),
  41.          ("Usage: hp-firmware [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  42.          utils.USAGE_ARGS,
  43.          utils.USAGE_DEVICE,
  44.          utils.USAGE_PRINTER,
  45.          utils.USAGE_SPACE,
  46.          utils.USAGE_OPTIONS,
  47.          ("Use USB IDs to specify printer:", "-s bbb:ddd, where bbb is the USB bus ID and ddd is the USB device ID. The ':' and all leading zeroes must be present.", "option", False),
  48.          ("Seconds to delay before download:", "-y<secs> or --delay=<secs> (float value, e.g. 0.5)", "option", False),
  49.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  50.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  51.          utils.USAGE_HELP,
  52.          utils.USAGE_SPACE,
  53.          utils.USAGE_NOTES,
  54.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  55.          utils.USAGE_SPACE,
  56.          utils.USAGE_SEEALSO,
  57.          ("hp-toolbox", "", "seealso", False),
  58.  
  59.          ]
  60.  
  61. def usage(typ='text'):
  62.     if typ == 'text':
  63.         utils.log_title(__title__, __version__)
  64.  
  65.     utils.format_text(USAGE, typ, __title__, 'hp-info', __version__)
  66.     sys.exit(0)
  67.  
  68.  
  69. log.set_module('hp-firmware')
  70.  
  71. try:
  72.  
  73.     try:
  74.         opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:gs:y:',
  75.             ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  76.              'help-desc', 'logging=', 'bus=', 'delay='])
  77.  
  78.     except getopt.GetoptError, e:
  79.         log.error(e.msg)
  80.         usage()
  81.  
  82.     printer_name = None
  83.     device_uri = None
  84.     log_level = logger.DEFAULT_LOG_LEVEL
  85.     bus = device.DEFAULT_PROBE_BUS
  86.     usb_bus_node = None
  87.     usb_bus_id = None
  88.     usb_device_id = None
  89.     silent = False
  90.     delay = 0.0
  91.  
  92.     if os.getenv("HPLIP_DEBUG"):
  93.         log.set_level('debug')
  94.  
  95.  
  96.     for o, a in opts:
  97.         if o in ('-h', '--help'):
  98.             usage()
  99.  
  100.         elif o == '--help-rest':
  101.             usage('rest')
  102.  
  103.         elif o == '--help-man':
  104.             usage('man')
  105.  
  106.         elif o == '--help-desc':
  107.             print __doc__,
  108.             sys.exit(0)
  109.  
  110.         elif o in ('-p', '--printer'):
  111.             if a.startswith('*'):
  112.                 printer_name = cups.getDefaultPrinter()
  113.                 log.debug(printer_name)
  114.                 
  115.                 if printer_name is not None:
  116.                     log.info("Using CUPS default printer: %s" % printer_name)
  117.                 else:
  118.                     log.error("CUPS default printer is not set.")
  119.                 
  120.             else:
  121.                 printer_name = a
  122.  
  123.         elif o in ('-d', '--device'):
  124.             device_uri = a
  125.  
  126.         elif o in ('-b', '--bus'):
  127.             bus = [x.lower().strip() for x in a.split(',')]
  128.             if not device.validateBusList(bus):
  129.                 usage()
  130.  
  131.         elif o in ('-l', '--logging'):
  132.             log_level = a.lower().strip()
  133.             if not log.set_level(log_level):
  134.                 usage()
  135.  
  136.         elif o == '-g':
  137.             log.set_level('debug')
  138.             
  139.         elif o == '-s':
  140.             silent = True
  141.             try:
  142.                 usb_bus_id, usb_device_id = a.split(":", 1)
  143.                 log.debug("USB bus ID: %s" % usb_bus_id)
  144.                 log.debug("USB device ID: %s" % usb_device_id)
  145.             except ValueError:
  146.                 log.error("Invalid USB IDs: %s" % a)
  147.                 sys.exit(1)
  148.                 
  149.             if len(usb_bus_id) != 3 or len(usb_device_id) != 3:
  150.                 log.error("Invalid USB IDs: %s" % a)
  151.                 sys.exit(1)
  152.                 
  153.             usb_bus_node = a
  154.             
  155.         elif o in ('-y', '--delay'):
  156.             try:
  157.                 delay = float(a)
  158.             except ValueError:
  159.                 log.error("Invalid delay value. Must be numeric (float) value. Setting delay to 0.0")
  160.                 delay = 0.0
  161.  
  162.  
  163.     if device_uri and printer_name:
  164.         log.error("You may not specify both a printer (-p) and a device (-d).")
  165.         usage()
  166.  
  167.     utils.log_title(__title__, __version__)
  168.     
  169.     if os.getuid() == 0:
  170.         log.warn("hp-firmware should not be run as root.")
  171.  
  172.     if silent:
  173.         # called by .rules file with -s bbb.ddd
  174.         printer_name = None
  175.         
  176.         if usb_bus_node is not None:
  177.             log.debug("USB bus node: %s" % usb_bus_node)
  178.             device_uri, sane_uri, fax_uri = device.makeURI(usb_bus_node, 1)
  179.             
  180.             if not device_uri:
  181.                 log.error("Invalid USB IDs: %s" % usb_bus_node)
  182.                 sys.exit(1)
  183.         
  184.     else:
  185.         if not device_uri and not printer_name:
  186.             try:
  187.                 device_uri = device.getInteractiveDeviceURI(bus, 
  188.                     filter={'fw-download' : (operator.gt, 0)})
  189.                     
  190.                 if device_uri is None:
  191.                     sys.exit(1)
  192.             except Error:
  193.                 log.error("Error occured during interactive mode. Exiting.")
  194.                 sys.exit(1)
  195.  
  196.     try:
  197.         d = device.Device(device_uri, printer_name)
  198.     except Error:
  199.         log.error("Error opening device. Exiting.")
  200.         sys.exit(1)
  201.  
  202.     if d.device_uri is None and printer_name:
  203.         log.error("Printer '%s' not found." % printer_name)
  204.         sys.exit(1)
  205.  
  206.     if d.device_uri is None and device_uri:
  207.         log.error("Malformed/invalid device-uri: %s" % device_uri)
  208.         sys.exit(1)
  209.  
  210.     user_cfg.last_used.device_uri = d.device_uri
  211.         
  212.        
  213.     try:
  214.         if delay:
  215.              time.sleep(delay)
  216.              
  217.         try:
  218.             d.open()
  219.             d.queryModel()
  220.         except Error, e:
  221.             log.error("Error opening device (%s). Exiting." % e.msg)
  222.             sys.exit(1)
  223.     
  224.         fw_download = d.mq.get('fw-download', 0)
  225.  
  226.         if fw_download:
  227.             if d.downloadFirmware(usb_bus_id, usb_device_id):
  228.                 if not silent:
  229.                     log.info("Done.")
  230.             else:
  231.                 log.error("Firmware download failed.")
  232.                 
  233.         else:
  234.             log.error("Device %s does not support or require firmware download." % device_uri)
  235.  
  236.     finally:
  237.         d.close()
  238.     
  239. except KeyboardInterrupt:
  240.     log.error("User exit")
  241.  
  242.  
  243.  
  244.  
  245.